package com.guldan.thrall.web.controller;

import com.guldan.core.constants.CommonConstant;
import com.guldan.core.dubbo.client.ResponseDTO;
import com.guldan.core.response.ResponseJson;
import com.guldan.core.utils.MathUtil;
import com.guldan.jaina.dto.datastat.home.*;
import com.guldan.jaina.dto.datastat.report.MoneyStatDTO;
import com.guldan.jaina.dto.datastat.report.ReportSearchDTO;
import com.guldan.jaina.dto.datastat.report.SubsidyGrantStatDTO;
import com.guldan.jaina.dto.report.Point;
import com.guldan.jaina.dto.report.Points;
import com.guldan.jaina.provider.IReportProvider;
import com.guldan.thrall.common.base.BaseController;
import com.guldan.thrall.dto.report.HomeDTO;
import com.guldan.thrall.dto.report.HomeReportSearchDTO;
import com.guldan.thrall.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Silas.
 * Date: 2016/12/11
 * Time: 22:58
 */
@Controller
@RequestMapping("/reportCenter")
public class ReportController extends BaseController {

    @Autowired
    private IReportProvider iReportProvider;


    //项目金额发放情况统计
    @RequestMapping(value = "/projectMoneyStat", method = RequestMethod.GET)
    @ResponseJson
    public
    @ResponseBody
    List<Points> list() {

        List<Points> pointsList = new ArrayList<>();

        Points points = new Points();
        points.setName("项目1");
        List<Point> pointList = new ArrayList<>();
        pointList.add(new Point("1月", 1000.0));
        pointList.add(new Point("2月", 2000.1));
        pointList.add(new Point("3月", 3000.33));
        pointList.add(new Point("4月", 222.0));
        pointList.add(new Point("5月", 433.5));
        points.setPointList(pointList);
        pointsList.add(points);

        Points points2 = new Points();
        points2.setName("项目2");
        List<Point> pointList2 = new ArrayList<>();
        pointList2.add(new Point("1月", 1000.0));
        pointList2.add(new Point("2月", 2000.1));
        pointList2.add(new Point("3月", 3000.33));
        pointList2.add(new Point("4月", 222.0));
        pointList2.add(new Point("5月", 433.5));
        points2.setPointList(pointList2);

        pointsList.add(points2);

        return pointsList;
        /*ResponseDTO<List<Points>> response = iReportProvider.projectMoneyStat(dto);
        return response.getData();*/
    }

    //获取首页快捷导航
    @RequestMapping(value = "/home/getHomeQuickNavList", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    List<HomeQuickNavDTO> getHomeQuickNavList() {
        ResponseDTO<List<HomeQuickNavDTO>> response = iReportProvider.getHomeQuickNavList();
        return response.getData();
    }

    //首页顶部一行统计
    @RequestMapping(value = "/home/getPart1DTO", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    Part1DTO getPart1DTO() {
        ResponseDTO<Part1DTO> response = iReportProvider.getPart1DTO();
        return response.getData();
    }

    //首页单项统计
    @RequestMapping(value = "/home/getHomeStatDTO", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    HomeDTO getHomeStatDTO() {
        ResponseDTO<Part1DTO> responsePart1 = iReportProvider.getPart1DTO();
        ResponseDTO<Part2DTO> responsePart2 = iReportProvider.getPart2DTO();
        HomeDTO homeDTO = new HomeDTO();
        homeDTO.setPart1DTO(responsePart1.getData());
        homeDTO.setPart2DTO(responsePart2.getData());
        return homeDTO;
    }

    //获取首页批次登记统计，按日汇总
    @RequestMapping(value = "/home/getBatchRegistStatByDate", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    List<Points> getBatchRegistStatByDate(@RequestBody HomeReportSearchDTO search) {
        ResponseDTO<List<BatchRegistStatDTO>> responseDTO = iReportProvider.getBatchRegistStatByDate(search.getHomeRegistSearchType(), search.getDateBegin(), search.getDateEnd());

        List<Points> pointsList = new ArrayList<>();

        Points points1 = new Points();
        Points points2 = new Points();

        List<Point> pointList1 = new ArrayList<>();
        List<Point> pointList2 = new ArrayList<>();

        switch (UserUtils.getUser().getUserRole()) {
            case CommonConstant.ROLE_TownsEntry:
                points1.setName("登记成功项目数");
                points2.setName("登记成功人次");
                break;
            case CommonConstant.ROLE_TownsReview:
                points1.setName("复核通过项目数");
                points2.setName("复核通过人次");
                break;
            case CommonConstant.ROLE_Org:
                points1.setName("审批通过项目数");
                points2.setName("审批通过人次");
                break;
            case CommonConstant.ROLE_Finance:
                points1.setName("审批通过项目数");
                points2.setName("审批通过人次");
                break;
            default:
                break;
        }

        for (BatchRegistStatDTO dto : responseDTO.getData()) {
            pointList1.add(new Point(dto.getDateStr(), dto.getBatchNum().doubleValue()));
            pointList2.add(new Point(dto.getDateStr(), dto.getRegisterNum().doubleValue()));
        }
        points1.setPointList(pointList1);
        points2.setPointList(pointList2);

        pointsList.add(points1);
        pointsList.add(points2);

        return pointsList;
    }

    //获取首页热门项目列表
    @RequestMapping(value = "/home/getHotProjectStatList", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    List<HotProjectStatDTO> getHotProjectStatList(@RequestBody HomeReportSearchDTO search) {
        ResponseDTO<List<HotProjectStatDTO>> responseDTO = iReportProvider.getHotProjectStatList();
        return responseDTO.getData();
    }




}
